package org.apache.commons.graph.builder

import org.apache.commons.graph.MutableGraph

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/**
 * TODO Fillme!!!
 *
 * @param <V> the Graph vertices type
 * @param <E> the Graph edges type
 * @param <G> the Graph type
</G></E></V> */
fun interface LinkedConnectionBuilder<V, E, G : MutableGraph<V, E>> {
    /**
     * Connects the graph.
     *
     * @param graphConnection the [GraphConnection]
     * @return the [org.apache.commons.graph.Graph] built from the connections.
     */
    fun withConnections(graphConnection: GraphConnection<V, E>): G
}


/**
 * TODO Fillme!!
 *
 * @param <V> the Graph vertices type
 * @param <E> the Graph edges type
 * @param <G> the Graph type
</G></E></V> */
/**
 * Creates a new [DefaultLinkedConnectionBuilder] for the graph.
 *
 * @param graph the graph
 */
class DefaultLinkedConnectionBuilder<V, E, G : MutableGraph<V, E>>(private val graph: G) : LinkedConnectionBuilder<V, E, G> {
    /**
     * {@inheritDoc}
     */
    override fun withConnections(graphConnection: GraphConnection<V, E>): G {
        ///var graphConnection = graphConnection
        //graphConnection = Assertions.checkNotNull(graphConnection, "Input graph cannot be configured with null connections")
        val grapher: GraphConnector<V, E> = DefaultGrapher(graph)
        graphConnection.connect(grapher)
        return graph
    }
}

fun <V, E, G : MutableGraph<V, E>> G.populate(): LinkedConnectionBuilder<V, E, G> = DefaultLinkedConnectionBuilder(this)

fun <V, E, G : MutableGraph<V, E>> G.populate(graphConnection: GraphConnection<V, E>) = populate().withConnections(graphConnection)

internal fun <V, E, G : MutableGraph<V, E>> G.useConnector(block : GraphConnector<V, E>.()-> Unit) = populate().withConnections{
    DefaultGrapher(this).block()
}
